/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.TX.CustomerIOTRPortal;

import session.TX.DishManagerSBRemote;
import session.TX.ShoppingCartSBRemote;
import session.TX.TransactionManagerSBRemote;
import session.TX.ULPSBRemote;
import Helper.Helper_LineItem;
import javax.faces.bean.ManagedBean;
import javax.ejb.EJB;
import javax.faces.event.ActionEvent;
import Helper.Helper_OutletFoodItem;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author FLKwek
 */
@ManagedBean(name = "FoodOrderManagerBean")
@ViewScoped
public class FoodOrderMB {

    @EJB
    private TransactionManagerSBRemote transactionManager;
    @EJB
    private ULPSBRemote loyaltyPointsManager;
    @EJB
    private DishManagerSBRemote dmbr;
    @ManagedProperty(value = "#{fakeMainPortalMB}")
    private FakeMainPortalMB portalManagedBean;
    @ManagedProperty(value = "#{cartBean}")
    private ShoppingCartMB cartJSFManagedBean;
    @ManagedProperty(value = "#{searchManagedBean}")
    private SearchMB searchManagedBean;
    
    private List<Helper_LineItem> orderForm = new ArrayList<Helper_LineItem>();
    private List<Helper_OutletFoodItem> orders_nonFestiveFoodItem = new ArrayList<Helper_OutletFoodItem>();
    private Helper_OutletFoodItem singleItem;
    private int defaultnumber = 0;
    private double totalprice;
    private String customerEmail;
    private String address;
    private Long prevOutletId;
    private int singleItemQuantity;

    /** Creates a new instance of FoodOrderMB */
    public FoodOrderMB() {
    }

    public void createOutlet(ActionEvent event) {
    }

    public int getDefaultnumber() {
        return defaultnumber;
    }

    public void setDefaultnumber(int defaultnumber) {
        this.defaultnumber = defaultnumber;
    }

    public double getTotalprice() {
        return totalprice;
    }

    public void setTotalprice(double totalprice) {
        this.totalprice = totalprice;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public FakeMainPortalMB getPortalManagedBean() {
        return portalManagedBean;
    }

    public void setPortalManagedBean(FakeMainPortalMB portalManagedBean) {
        this.portalManagedBean = portalManagedBean;
    }

    public Long getPrevOutletId() {
        return prevOutletId;
    }

    public void setPrevOutletId(Long prevOutletId) {
        this.prevOutletId = prevOutletId;
    }

    public Helper_OutletFoodItem getSingleItem() {
        return singleItem;
    }

    public void setSingleItem(Helper_OutletFoodItem singleItem) {
        this.singleItem = singleItem;
    }

    public SearchMB getSearchManagedBean() {
        return searchManagedBean;
    }

    public void setSearchManagedBean(SearchMB searchManagedBean) {
        this.searchManagedBean = searchManagedBean;
    }

    public List<Helper_OutletFoodItem> getNDFoodItemForSale() {
        if (prevOutletId == null || 
                !(prevOutletId.longValue() == portalManagedBean.getOutletId().longValue())) {
            orders_nonFestiveFoodItem = dmbr.getNonDeliItemHelperForSale(portalManagedBean.getOutletId());
        }
        prevOutletId = portalManagedBean.getOutletId();

        return orders_nonFestiveFoodItem;
    }

    public List<Helper_LineItem> getAllNDPurchases() {
        List<Helper_LineItem> output = new ArrayList<Helper_LineItem>();
        for (Helper_OutletFoodItem item : orders_nonFestiveFoodItem) {
            if (item.getQty() > 0) {
                output.add(new Helper_LineItem(item.getFoodItemId(), item.getFoodItemName(),
                        item.getQty(), item.getPrice(), item.getStakeholderName(), item.getOutletId(),
                        item.getOutletName()));
                //System.out.println(item);
            }
        }
        orderForm = output;
        return orderForm;
    }

    //Run through all the order forms, sum up the prices to get subtotal
    public double getSubtotal() {
        totalprice = 0.0;

        for (Helper_OutletFoodItem item : orders_nonFestiveFoodItem) {
            if (item.getQty() > 0) {
                totalprice += item.getPrice() * item.getQty();
            }
        }
        return totalprice;
    }
    
    
    //Randall: 13-oct-2011 1923hrs
    //Method to add a single item into the shopping cart
    public void addToCart_singleItem(){
        ShoppingCartSBRemote cartManager = portalManagedBean.getCartManager();
        Helper_LineItem item = new Helper_LineItem(
              singleItem.getFoodItemId(),
              singleItem.getFoodItemName(),
              singleItemQuantity,
              singleItem.getPrice(),
              singleItem.getStakeholderName(),
              singleItem.getOutletId(),
              singleItem.getOutletName()
            );
        getCartJSFManagedBean().setCart(null);
        
        if(singleItemQuantity>0) cartManager.cart_addSingleItem(item);
    }//-----------------------------------------------------------------------------------------
    
    //Randall: 13-oct-2011 1923hrs
    //Method to add the contents of  into the shopping cart
    public void addToCart_multipleItem(){
        ShoppingCartSBRemote cartManager = portalManagedBean.getCartManager();
        
        getCartJSFManagedBean().setCart(null);
        cartManager.cart_addMultipleItems(this.getAllNDPurchases());
        
    }//-----------------------------------------------------------------------------------------
    
    

    public String getCustomerEmail() {
        return customerEmail;
    }

    public void setCustomerEmail(String customerEmail) {
        this.customerEmail = customerEmail;
    }

    public ShoppingCartMB getCartJSFManagedBean() {
        return cartJSFManagedBean;
    }

    public void setCartJSFManagedBean(ShoppingCartMB cartJSFManagedBean) {
        this.cartJSFManagedBean = cartJSFManagedBean;
    }

    public int getSingleItemQuantity() {
        return singleItemQuantity;
    }

    public void setSingleItemQuantity(int singleItemQuantity) {
        this.singleItemQuantity = singleItemQuantity;
    }
    
    
}
